package conexionWS;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class serviciosWS {

	String NAMESPACE="http://WebServiceIA/";
	String URL="http://172.24.180.75:8080/IA/WebServiceIAService?WSDL";
	
	public serviciosWS() {}
	
	public String inicioSesion(String nombre, String clave){
		String NAMESPACE="http://WebServiceIA/";
		String URL="http://172.24.180.75:8080/IA/WebServiceIAService?WSDL";
		String METHOD_NAME="iniciar_sesion";
		String SOAP_ACTION= "iniciar_sesion";		
		SoapObject request;		
		request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("usuario", nombre);
		request.addProperty("contrasenna", clave);
		Object result;
		try {
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet=false;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		//androidHttpTransport.debug=true;
		androidHttpTransport.call(SOAP_ACTION, envelope);
		result = (Object)envelope.getResponse();
		return result.toString();
		}
		catch (Exception e)
		{
		return e.toString();
		}
	}
	
	/**
	 * Solicita al Web Service las partidas disponibles.
	 * @param nombreUsuario
	 * @return Todas las partidas disponibles
	 */
	public String registroUsuario(String nombre, String clave, String email){
		String METHOD_NAME="crear_usuario";
		String SOAP_ACTION= "crear_usuario";		
		SoapObject request;		
		request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("usuario", nombre);
		request.addProperty("clave", clave);
		request.addProperty("verificacion", clave);
		request.addProperty("nombre_completo", "IDIOTAS");
		request.addProperty("correo", email);
		Object result;
		try {
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet=false;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		//androidHttpTransport.debug=true;
		androidHttpTransport.call(SOAP_ACTION, envelope);
		result = (Object)envelope.getResponse();
		return result.toString();
		}
		catch (Exception e)
		{
		return e.toString();
		}
	}
	
	public String crearPartidaJugadorVsJugador(String nombre, String ficha){
		String METHOD_NAME="crear_partida_vs_j2";
		String SOAP_ACTION= "crear_partida_vs_j2";		
		SoapObject request;		
		request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("usuario", nombre);
		request.addProperty("color_piezas", ficha);
		Object result;
		try {
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet=false;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		//androidHttpTransport.debug=true;
		androidHttpTransport.call(SOAP_ACTION, envelope);
		result = (Object)envelope.getResponse();
		return result.toString();
		}
		catch (Exception e)
		{
		return e.toString();
		}
	}
	
	public String crearPartidaJugadorVsPC(String nombre, String ficha){
		String METHOD_NAME="crear_partida_vs_pc";
		String SOAP_ACTION= "crear_partida_vs_pc";		
		SoapObject request;		
		request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("usuario", nombre);
		request.addProperty("color_piezas", ficha);
		Object result;
		try {
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet=false;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		//androidHttpTransport.debug=true;
		androidHttpTransport.call(SOAP_ACTION, envelope);
		result = (Object)envelope.getResponse();
		return result.toString();
		}
		catch (Exception e)
		{
		return e.toString();
		}
	}
	
	public String mover(String id_partida, String usuario, String movimiento){
		String METHOD_NAME="mover_pieza";
		String SOAP_ACTION= "mover_pieza";		
		SoapObject request;		
		request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("id_partida", id_partida);
		request.addProperty("usuario", usuario);
		request.addProperty("movimiento", movimiento);
		Object result;
		try {
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet=false;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		//androidHttpTransport.debug=true;
		androidHttpTransport.call(SOAP_ACTION, envelope);
		result = (Object)envelope.getResponse();
		return result.toString();
		}
		catch (Exception e)
		{
		return e.toString();
		}
	}
	
	public String obtenerTurno(String id_partida, String usuario){
		String METHOD_NAME="obtener_turno";
		String SOAP_ACTION= "obtener_turno";		
		SoapObject request;		
		request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("id_partida", id_partida);
		request.addProperty("usuario", usuario);
		Object result;
		try {
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet=false;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		//androidHttpTransport.debug=true;
		androidHttpTransport.call(SOAP_ACTION, envelope);
		result = (Object)envelope.getResponse();
		return result.toString();
		}
		catch (Exception e)
		{
		return e.toString();
		}
	}
	
	public String obtenerPartidasJugables(String usuario){
		String METHOD_NAME="obtener_datos_partidas_jugables";
		String SOAP_ACTION= "obtener_datos_partidas_jugables";		
		SoapObject request;		
		request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("usuario", usuario);
		Object result;
		try {
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet=false;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
		//androidHttpTransport.debug=true;
		androidHttpTransport.call(SOAP_ACTION, envelope);
		result = (Object)envelope.getResponse();
		return result.toString();
		}
		catch (Exception e)
		{
		return e.toString();
		}
	}
	

}
